import * as request from '../constant/fetch-request'
import HTTP_CODE from '../constant/http-code'
import {message} from 'antd'
import {convertContent} from '../constant/lang-util'

export const getLogicQuizzes = (page = 1, pageSize = 10, searchConditions = {}) => {
  const {id, userId, description} = searchConditions
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/v3/logicQuizzes/pageable?page=${--page}&pageSize=${pageSize}
      ${id ? '&quizId=' + id : ''}
      ${userId ? '&userId=' + userId : ''}
      ${description ? '&description=' + description : ''}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch({
          type: 'GET_LOGIC_QUIZZES',
          data: res.body
        })
      }
    })()
  }
}

export const addLogicQuiz = (quiz, callback) => {
  return dispatch => {
    (async () => {
      const res = await request.post(`./api/v3/logicQuizzes`, quiz)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('批量创建逻辑题成功'))
        callback()
      }
    })()
  }
}

export const deleteQuiz = (quizId) => {
  return dispatch => {
    (async () => {
      const res = await request.del(`./api/v3/logicQuizzes/${quizId}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.success(convertContent('删除成功'))
        dispatch(getLogicQuizzes())
      }
    })()
  }
}
